﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace RSSRadio
{
    public partial class Settings : Form
    {
        String cardFolder;
        String deviceFolder;
        bool statusOk = true;

        public Settings()
        {
            InitializeComponent();
            if (AppSettings.hasStorageCard())
            {
                cardFolder = AppSettings.getStorageCardPodcastFolder();
            }
            else
            {
                cardFolder = "-- INVALID FOLDER NAME --";
            }
            deviceFolder = AppSettings.getDevicePodcastFolder();

            textBoxPodcastPath.Text = Globals.AppSettings.DownloadedPodcastDir;

            setRadioButtons();

            numericRetention.Value = Globals.AppSettings.podcastExpirationDays;
        }

        private void setRadioButtons()
        {
            if (textBoxPodcastPath.Text == deviceFolder)
            {
                rdDevice.Checked = true;
                textBoxPodcastPath.Enabled = false;
            }
            else if (textBoxPodcastPath.Text == cardFolder)
            {
                rdCard.Checked = true;
                textBoxPodcastPath.Enabled = false;
            }
            else             
            {
                rdCustom.Checked = true;
                textBoxPodcastPath.Enabled = true;
            }
            rdCard.Enabled = AppSettings.hasStorageCard();
            
        }

        private void setPathTextBox()
        {
            if (rdCustom.Checked)
            {
                textBoxPodcastPath.Enabled = true;
            }
            else if (rdCard.Checked)
            {
                textBoxPodcastPath.Text = cardFolder;
                textBoxPodcastPath.Enabled = false;
            }
            else if (rdDevice.Checked)
            {
                textBoxPodcastPath.Text = deviceFolder;
                textBoxPodcastPath.Enabled = false;
            }
        }

        private void Settings_Closed(object sender, EventArgs e)
        {
            if (statusOk)
            {
                bool changed = false;
                if (Globals.AppSettings.DownloadedPodcastDir != textBoxPodcastPath.Text)
                {
                    Globals.AppSettings.DownloadedPodcastDir = textBoxPodcastPath.Text;
                    changed = true;
                }
                if (Globals.AppSettings.podcastExpirationDays != (int)numericRetention.Value)
                {
                    Globals.AppSettings.podcastExpirationDays = (int)numericRetention.Value;
                    changed = true;
                }
                if (changed)
                {
                    Globals.AppSettings.Save();
                    MessageBox.Show("You need to restart program in order for changes to take effect");
                }
            }
        }

        private void rdPath_CheckedChanged(object sender, EventArgs e)
        {
            setPathTextBox();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            statusOk = false;
            this.Close();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            statusOk = true;
            this.Close();
        }

    }
}